package com.rjwh.dingdong.client.util;

import com.aishua.lib.utils.AsStrUtils;
import com.alibaba.fastjson.JSON;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResBaseBean;
import com.rjwh.dingdong.client.constant.LocalConstant;

import net.tsz.afinal.http.AjaxParams;

import java.io.FileNotFoundException;

public class UploadUtil {

    public static String uploadPic(final String picPath, String modularid) {
        AjaxParams params = new AjaxParams();
        // 上传文件
        params.put("modularid", modularid);
        String newFilePath = FileUtils.getTempImageStorageDirectory() + System.currentTimeMillis() + ".png";
        try {
            params.put("filedata", BitmapFileUtil.compressBmpToFile(90, picPath, newFilePath));
            LogUtil.e("UploadUtil", "pic: " + BitmapFileUtil.compressBmpToFile(90, picPath, newFilePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String aid = "";
        try {
            Object postSync = MyApplication.http.postSync(
                    LocalConstant.SEND_UPLOAD_ATTACH, params);
            LogUtil.e("UploadPic", " params : " + params);
            if (postSync != null) {
                String result = postSync.toString();
                LogUtil.e("UploadPic", "result: " + result);
                ResBaseBean resBean = JSON.parseObject(result,
                        ResBaseBean.class);
                String state = resBean.getExeustate().getState();
                String msg = resBean.getExeustate().getMsg();
                if (!AsStrUtils.isEmpty(state) && !AsStrUtils.isEmpty(msg) && state.equals("1")) {
                    aid = msg;
                }
            }
        } catch (Exception e) {
            LogUtil.e("UploadUtil", "e:" + e);
            e.printStackTrace();
        }
        return aid;
    }
}
